/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bkav.portal.portlet.product.service.impl;

import java.util.Date;

import java.util.List;

import com.bkav.portal.portlet.product.model.Product;
import com.bkav.portal.portlet.product.model.ProductCategory;
import com.bkav.portal.portlet.product.service.ProductCategoryLocalServiceUtil;
import com.bkav.portal.portlet.product.service.ProductLocalServiceUtil;
import com.bkav.portal.portlet.product.service.base.ProductCategoryLocalServiceBaseImpl;
import com.bkav.portal.portlet.product.service.persistence.ProductCategoryFinder;
import com.bkav.portal.portlet.product.service.persistence.ProductCategoryFinderUtil;
import com.bkav.portal.portlet.product.service.persistence.ProductCategoryPersistence;
import com.bkav.portal.portlet.product.service.persistence.ProductCategoryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;
import com.liferay.portal.service.ServiceContext;
import com.bkav.portal.portlet.product.model.ProductCategory;

/**
 * The implementation of the product category local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link com.bkav.portal.portlet.product.service.ProductCategoryLocalService}
 * interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 *
 * @author ducdvd
 * @see com.bkav.portal.portlet.product.service.base.ProductCategoryLocalServiceBaseImpl
 * @see com.bkav.portal.portlet.product.service.ProductCategoryLocalServiceUtil
 */
public class ProductCategoryLocalServiceImpl extends
		ProductCategoryLocalServiceBaseImpl {
	
	public int countAll() throws SystemException{
		return productCategoryPersistence.countAll();
	}
	
	public int countByCompany(long companyId) throws SystemException{
		return productCategoryPersistence.countByCompany(companyId);
	}
	
	public int countByGroup(long groupId) throws SystemException {
		return productCategoryPersistence.countByGroup(groupId);
	}
	
	public List<ProductCategory> findAll() throws SystemException {
		return productCategoryPersistence.findAll();
	}
	
	public List<ProductCategory> findByCompany(long companyId) throws SystemException {
		return productCategoryPersistence.findByCompany(companyId);
	}
	
	public List<ProductCategory> findByCompany(long companyId, int start, int end) throws SystemException {
		return productCategoryPersistence.findByCompany(companyId, start, end);
	}
	
	public List<ProductCategory> findByGroup(long groupId) throws SystemException {
		return productCategoryPersistence.findByGroup(groupId);
	}
	
	public List<ProductCategory> findByGroup(long groupId, int start, int end)
			throws SystemException {
		return productCategoryPersistence.findByGroup(groupId, start, end);
	}
	
	public List<ProductCategory> findByKeyword(long groupId, String keyword,
			int start, int end) throws SystemException{
		return ProductCategoryFinderUtil.findByKeyword(groupId, keyword, start, end);
	}
	
	
	public ProductCategory addProductCategory(long companyId, long groupId,
			long userId, String userName, String categoryName,
			String categoryInfo) throws SystemException,
			PortalException {

		Date now = new Date();

		long categoryId = counterLocalService.increment();

		ProductCategory productCategory = productCategoryPersistence.create(categoryId);

		productCategory.setCompanyId(companyId);
		productCategory.setGroupId(groupId);
		productCategory.setUserId(userId);
		productCategory.setUserName(userName);
		productCategory.setCategoryName(categoryName);
		productCategory.setCategoryInfo(categoryInfo);
		productCategory.setCreateDate(now);
		productCategory.setModifiedDate(now);

		productCategoryPersistence.update(productCategory, false);

		
		return productCategory;
	}
	
	public ProductCategory updateProductCategory(long companyId, long groupId,
			long userId, String userName, long categoryId, String categoryName,
			String categoryInfo) throws SystemException,
			PortalException {

		Date now = new Date();

		ProductCategory productCategory = productCategoryPersistence.fetchByPrimaryKey(categoryId);

		productCategory.setCompanyId(companyId);
		productCategory.setGroupId(groupId);
		productCategory.setUserId(userId);
		productCategory.setUserName(userName);
		productCategory.setCategoryName(categoryName);
		productCategory.setCategoryInfo(categoryInfo);
		productCategory.setModifiedDate(now);

		productCategoryPersistence.update(productCategory, false);

		return productCategory;
	}

	public void deleteProductCategory(long categoryId, boolean deleteCategory)
			throws SystemException, PortalException {

		if (categoryId > 0) {

			ProductCategory productCategory = getProductCategory(categoryId);

			if (productCategory != null) {

				List<Product> productList = ProductLocalServiceUtil.findByCategory(categoryId);

				if (deleteCategory && !productList.isEmpty()) {// Truong hop xoa
																// tat ca cac
																// lien ket
																// thuoc chuyen
																// muc

					for (Product product : productList) {
						// Xoa tat ca cac lien ket
						ProductLocalServiceUtil.deleteProduct(product.getProductId());
					}
				} else if (!deleteCategory && !productList.isEmpty()) {// Truong
																		// hop
																		// khong
																		// xoa
																		// cac
																		// lien
																		// ket
																		// thuoc
																		// chuyen
																		// muc

					for (Product product : productList) {
						// Cap nhat Id chuyen muc cua lien ket = 0
						product.setCategoryId(0);
						ProductLocalServiceUtil.updateProduct(product, false);
					}
				}

				// Xoa chuyen muc
				productCategoryPersistence.remove(categoryId);


			}
		}
	}
}